package com.team6.yygh.hosp.controller.api;


import com.team6.yygh.exception.YyghException;
import com.team6.yygh.helper.HttpRequestHelper;
import com.team6.yygh.hosp.service.DepartmentService;
import com.team6.yygh.hosp.service.HospitalService;
import com.team6.yygh.hosp.service.HospitalSetService;
import com.team6.yygh.hosp.service.ScheduleService;
import com.team6.yygh.model.hosp.Department;
import com.team6.yygh.model.hosp.Hospital;
import com.team6.yygh.model.hosp.Schedule;
import com.team6.yygh.result.Result;
import com.team6.yygh.result.ResultCodeEnum;
import com.team6.yygh.utils.MD5;
import com.team6.yygh.vo.hosp.DepartmentQueryVo;
import com.team6.yygh.vo.hosp.ScheduleOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName ApiController
 * @Author ZhuYouWan
 * @Date 2022/10/19 10:15
 **/
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 删除排班信息
     */
    @ApiOperation(value = "删除排班信息")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        // 获取传递进来的排班信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取传递进来的医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取传递进来的排班编号
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        // 获取医院系统传递进来的签名，签名已进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递的医院编号，查询数据库， 查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 数据库查询出来的签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    /**
     * 查询排班信息
     */
    @ApiOperation(value = "查询排班信息")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        // 获取传递进来的排班信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取传递进来的医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取传递进来的科室编号
        String depcode = (String) paramMap.get("depcode");
        // 当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));
        // 获取医院系统传递进来的签名，签名已进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递的医院编号，查询数据库， 查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 数据库查询出来的签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        ScheduleOrderVo scheduleOrderVo = new ScheduleOrderVo();
        scheduleOrderVo.setHoscode(hoscode);
        scheduleOrderVo.setDepcode(depcode);
        // 调用方法查询
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleOrderVo);
        return Result.ok(pageModel);
    }

    /**
     * 上传排班信息
     */
    @ApiOperation(value = "上传排班信息")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        // 获取传递进来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取传递进来的医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取医院系统传递进来的签名，签名已进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递的医院编号，查询数据库， 查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 数据库查询出来的签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 删除科室接口
     */
    @ApiOperation(value = "删除科室接口")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        // 获取传递进来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取传递进来的医院编号与科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        // 获取医院系统传递进来的签名，签名已进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递的医院编号，查询数据库， 查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 数据库查询出来的签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 查询科室信息接口
     */
    @ApiOperation(value = "查询科室信息接口")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        // 获取传递进来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取传递进来的医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));
        // 获取医院系统传递进来的签名，签名已进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递的医院编号，查询数据库， 查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 数据库查询出来的签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        // 调用方法查询
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 上传科室接口
     */
    @ApiOperation(value = "上传科室接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        // 获取传递进来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取传递进来的医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取医院系统传递进来的签名，签名已进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递的医院编号，查询数据库， 查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 数据库查询出来的签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用方法进行添加
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * 查询医院
     */
    @ApiOperation(value = "查询医院")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        // 获取传递进来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取传递进来的医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取医院系统传递进来的签名，签名已进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递的医院编号，查询数据库， 查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 数据库查询出来的签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传医院接口
     */
    @ApiOperation(value = "上传医院接口")
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        // 获取传递进来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院系统传递进来的签名，签名已进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递的医院编号，查询数据库， 查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 数据库查询出来的签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 传输过程中"+"转换为" ",因此需要进行转换
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replace(" ","+");
        paramMap.put("logoData", logoData);
        // 存入数据库
        hospitalService.save(paramMap);
        return Result.ok();
    }
}
